// TODO: Add color

precision mediump float;       	// Set the default precision to medium. We don't need as high of a 
								// precision in the fragment shader.
uniform vec4 u_LightPos[16];    // The position of the light in eye space.
uniform vec4 u_LightCol[16];    // The color of the light.
uniform int u_LightCount;		// The number of lights being rendered.
uniform sampler2D u_Texture;    // The input texture.
  
varying vec3 v_Position;		// Interpolated position for this fragment.
varying vec4 v_Color;          	// This is the color from the vertex shader interpolated across the 
  								// triangle per fragment.
varying vec3 v_Normal;         	// Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.
  
// The entry point for our fragment shader.
void main() {
	float color = 0.0;
	int i;
	
	for (i = 0; i < u_LightCount && i < 16; i++) {
	  // Will be used for attenuation.
      float distance = length(u_LightPos[i].xyz - v_Position);                  

	  // Get a lighting direction vector from the light to the vertex.
      vec3 lightVector = normalize(u_LightPos[i].xyz - v_Position);              	

	  // Calculate the dot product of the light vector and vertex normal. 
	  // If the normal and light vector are pointing in the same direction 
	  // then it will get max illumination.
      float diffuse = max(dot(v_Normal, lightVector), 0.0);               	  		  													  

	  // Add attenuation. 
      diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));
    
      color = color + diffuse;  
    } 
    
    // Add ambient lighting
    color = color + 0.3;

	// Multiply the color by the diffuse illumination level and texture value to get final output color.
	gl_FragColor = (v_Color * color * texture2D(u_Texture, v_TexCoordinate));                                  		
}                                                                     	
